<template>
	<view class="home">
		<view class="box" style="background-image: url('https://applet.atptest.com/pccshomeimage.jpg?time=240830');">

			<!-- 白背景 -->
			<view class="white_box"></view>

			<!-- 功能按钮 -->
			<view class="btn_list">
				<view v-for="(item,index) in btn" :key="index" @click="btnClick(index)">
					<image :src="item.img" mode="widthFix" class="img"></image>
					<view>{{item.name}}</view>
				</view>
			</view>
		</view>
		<!-- 立即检测 -->
		<view class="textBtn" @click="textClick()">
			<image src="../../static/img/main/pointer.png" mode="widthFix"></image>
			<view class="text">
				<view style="font-size: 32rpx;">立即检测</view>
				<view style="font-size: 16rpx;">Check Now</view>
			</view>
		</view>

		<!-- 资讯 -->
		<!-- <view class="message_container">
			<image style="width: 100%;" src="../../static/img/main/news.png" mode="widthFix"></image>
			<view class="news" v-for="(item,index) in newsList" :key="index" @click="toNewsDetail(item.id)">
				<view class="left">
					<view class="newsTitle">
						{{item.title}}
					</view>
					<view style="color: #acacac;display: flex;justify-content: space-between;">
						<view>{{item.author}}</view>
						<view>{{item.publicationTime}}</view>
					</view>
				</view>
				<view class="right">
					<image :src="item.img" mode="" style="border-radius: 10rpx;"></image>
				</view>
			</view>
			<view v-if="bottomShow">
				<u-divider text="没有更多了"></u-divider>
			</view>
		</view> -->

		<u-modal :show="show" @confirm="toCoupon" @cancel="show=false" confirmText="去查看" cancelText="取消"
			:showCancelButton="true" :asyncClose="true" title="优惠券消息" :content='currentTitle'></u-modal>

		<AccreditModal v-if="accreditFlag" @closeAccrredit='closeAccrredit' />
	</view>
</template>

<script>
	import {
		getCodeByLink,
		giveCouponById,
		getNewCouponsCount,
		PageList_Article,
		GetUserInfo,
	} from '../../api/user.js'
	import AccreditModal from "../accredit/accreditModal.vue"
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		components: {
			AccreditModal
		},
		data() {
			return {
				currentTitle: '',
				code: '',
				show: false,
				accreditFlag: false,
				shareCode: '', // 分享的code
				imgUrl: '',
				btn: [{
						name: "关于我们",
						img: require('../../static/img/main/icon/icon1.png')
					},
					{
						name: "会员中心",
						img: require('../../static/img/main/icon/icon2.png')
					},
					{
						name: "检测订单",
						img: require('../../static/img/main/icon/icon3.png')
					},
					{
						name: "联系我们",
						img: require('../../static/img/main/icon/icon4.png')
					}
				],
				newsList: [],
				size: 5,
				pageIndex: 1,
				bottomShow: false,
				agentId: "",
				openId: "",
			};
		},
		computed: {
			...mapState('user', ['userInfo', 'token']) // vuex
		},
		// 上拉加载
		onReachBottom() {
			this.pageIndex++
			this.getNewslist({
				size: this.size,
				pageIndex: this.pageIndex,
			})
		},

		onShow() {
			// wx.getUserInfo({
			// 	success: function(res) {
			// 		console.log("用户信息1：",res);
			// 	}
			// })
			// return
			this._getNewCouponsCount() // 不管是不是新用户和老用户 赠送的逻辑
			this.getUserInfo()
			this.getNewslist({
				size: this.size,
				pageIndex: this.pageIndex,
			})
		},

		onLoad(options) {
			console.log("options", options);
			// uni.navigateTo({
			// 	url:"/pagesB/pages/bargain/bargain" 
			// })
			// return 
			// 用户点击小程序分享的优惠券 在这里获取code 用户后续转送操作 
			if (options.code) {
				this.shareCode = options.code
				this.getLinks(options.code)
			}
			//新用户通过分享的二维码/短链接进来，自动分配到指定的业务员或分销商下
			if (options.agentId) { //短链接
				this.agentId = options.agentId
			}
			if (options.scene) { //二维码
				const scene = decodeURIComponent(options.scene);
				console.log("scene", scene);
				this.agentId = scene;
			}
			// token不存在，调小程序登陆接口获取code,通过code获取token
			if (!uni.getStorageSync('token')) {
				this.getInfo()
			}

		},

		onHide() {
			this.pageIndex = 1
			this.newsList = []
		},
		methods: {
			...mapMutations('user', ['setUserInfo', 'setToken']),

			textClick() {
				uni.switchTab({
					url: '/pages/test/test'
				})
			},

			// 获取用户信息
			getUserInfo() {
				GetUserInfo().then((res) => {
					console.log("用户信息", res);
					if (!res.companyPhone) {
						this.accreditFlag = true;
					} else {
						this.accreditFlag = false;
					}
				})
			},
			//关闭授权弹框
			closeAccrredit(val) {
				console.log("关闭", val);
				this.accreditFlag = val
			},
			btnClick(i) {
				if (i == 0) {
					uni.navigateTo({
						url: '/pagesA/pages/intro/intro'
					})
				} else if (i == 3) {
					uni.navigateTo({
						url: '/pagesA/pages/contact/contact'
					})
				} else if (i == 2) {
					uni.navigateTo({
						url: '/pagesB/pages/order/order?i=99'
					})
				} else if (i == 1) {
					uni.navigateTo({
						url: '/pagesB/pages/memberCenter/memberCenter'
					})
				}
			},

			// 获取token
			getInfo() {
				uni.login({
					provider: 'weixin',
					success: (res) => {
						console.log("登录code", res);
						this.code = res.code
						this.getToken()
					}
				})
			},

			// 获取token
			async getToken() {
				const _that = this
				uni.request({
					url: 'https://wechat.atptest.com:44386/connect/token',
					data: {
						code: _that.code,
						grant_type: 'wechat',
						AgentId: _that.agentId
					},
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'Authorization': 'Basic QXBpX1dlYjpBcGlfV2Vi'
					},
					success(res) {
						_that.setToken(res.data.access_token)
					}
				})
			},

			// 根据获取到的分享用户点击进来携带的code 来获取链接地址
			async getLinks(x) {
				let result = await getCodeByLink({
					code: x
				})
				if (result.url) {
					let y = result.url.split('=')
					const couponPersonId = y[1]
					let data = {
						couponPersonId,
						code: this.shareCode
					}
					// 唤起赠送优惠券的接口
					let res = await giveCouponById(data)
					if (res == true) {
						this._getNewCouponsCount() //判断是否有新的优惠券 执行弹窗
					} else {
						return uni.showToast({
							title: res.message,
							icon: 'error'
						})
					}
				} else {
					// console.error(result);
				}
			},

			// 跳转到优惠券广场
			toCoupon() {
				this.show = false
				uni.navigateTo({
					url: '/pagesB/pages/memberCenter/memberCenter?state=0'
				})
			},

			// 判断是否有新的优惠券 执行弹窗
			async _getNewCouponsCount() {
				let data = await getNewCouponsCount()
				if (data) {
					this.show = true
					this.currentTitle = `恭喜您获得${data}张新的优惠券，期待您的使用。`
				}
			},
			// 获取新闻列表
			getNewslist(e) {
				PageList_Article(e).then((res) => {
					if (res == null) {
						this.bottomShow = true
						return
					}
					this.newsList = this.newsList.concat(res.items)
				})
			},
			// 跳转新闻详情
			toNewsDetail(id) {
				uni.navigateTo({
					url: `/pagesA/pages/newsDetails/newsDetails?id=${id}`
				})
			}
		},


	}
</script>

<style lang="scss" scoped>
	.home {
		font-size: 24rpx;
		background-color: #f5f5f5;

		.box {
			position: relative;
			width: 100%;
			height: 100vh;
			background-size: cover;
			background-repeat: no-repeat;
			background-position: center;

			.white_box {
				width: 100%;
				height: 350rpx;
				position: absolute;
				left: 0;
				bottom: 0;
				background-color: #f5f5f5;
				border-top-left-radius: 10rpx;
				border-top-right-radius: 10rpx;
			}

			.btn_list {
				position: absolute;
				left: 0;
				bottom: 40rpx;
				width: 100%;
				display: flex;
				justify-content: space-around;
				text-align: center;
				background-color: #f5f5f5;

				.img {
					width: 100rpx;
				}
			}
		}

		.textBtn {
			position: absolute;
			width: 90%;
			height: 100rpx;
			border-radius: 24rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background: linear-gradient(270.00deg, rgb(1, 25, 110) 0%, rgb(63, 89, 191) 100%);
			left: 50%;
			bottom: 160rpx;
			transform: translate(-50%, -50%);

			image {
				width: 60rpx;
			}

			.text {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				color: #eee;
				margin-left: 10rpx;
			}
		}

		.main {
			text-align: center;
			padding: 0 40rpx;

			.swiper {
				height: 350rpx;

				image {
					border-radius: 10px;
					width: 100%;
				}
			}
		}

		.message_container {}

		.news {
			display: flex;
			justify-content: space-between;
			margin: 0 20rpx;
			padding: 10rpx;
			margin-bottom: 20rpx;
			border-radius: 10rpx;
			box-shadow: 0px 1px 4px rgba(0, 0, 0, 0.25);

			.left {
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				padding: 10rpx 0 0;

				.newsTitle {
					width: 460rpx !important;
					line-height: 48rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					width: 100%;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 3;
					word-break: break-all;
				}
			}

			.right {
				width: 200rpx;
				height: 200rpx;
				padding-left: 10rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}
	}

	// 文字超过三行省略号
	.textOVerThree {
		display: -webkit-box;
		overflow: hidden;
		white-space: normal !important;
		text-overflow: ellipsis;
		word-wrap: break-word;
		-webkit-line-clamp: 3;
		-webkit-box-orient: vertical;
		line-height: 12px;
	}
</style>